package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.annotation.IsLoginAnno;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.exception.CustomException;
import cn.wolfcode.p2p.base.query.VideoAuthQuery;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class VideoAuthController {
        @Autowired
        private IVideoAuthService videoAuthService;

        @RequestMapping("videoAuth")
        @IsLoginAnno
        public String videoAuth(@ModelAttribute("qo")VideoAuthQuery qo, Model model){

                //只能审核管理员自己的审核请求,除了超级管理员之外
                LoginInfo loginInfo = UserContext.getLoginInfo();
                if(!"admin".equals(loginInfo.getUsername())){
                        qo.setAuditorId(loginInfo.getId());
                }
                PageResult pageResult = videoAuthService.query(qo);
                model.addAttribute("pageResult",pageResult);
                return "videoAuth/list";
        }

        @ResponseBody
        @RequestMapping("videoAuth_audit")
        public JSONResult videoAuthAudit(Long id,String remark,Integer state){
                JSONResult jsonResult = new JSONResult();
                try {
                        videoAuthService.videoAuthAudit(id,remark,state);
                    }catch (CustomException e ){
                        jsonResult.setMsg(e.getMessage());//自定义的异常,避免所有异常都返回同样的信息给客户
                    } catch (Exception e) {
                        e.printStackTrace();
                        jsonResult.setMsg("系统繁忙,请稍后再试");
                }
                 return jsonResult;
        }


}
